package com.cloudptt.wp.ui.address.adapter;

import java.util.Arrays;
import java.util.List;

import com.cloudptt.wp.R;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TrackContactAdapter extends BaseAdapter {
	

	private Context context;
	private List<UserInfo> list = null;
	private boolean [] states = null;
	private static final int[] BACKGROUNDS = new int[]{
			R.drawable.state_selected_black,
			R.drawable.state_selected_blue,
			R.drawable.state_selected_cyan,
			R.drawable.state_selected_green,
			R.drawable.state_selected_magenta,
			R.drawable.state_selected_orange,
			R.drawable.state_selected_purple,
			R.drawable.state_selected_red,
			R.drawable.state_selected_brilliant,
			R.drawable.state_selected_yellow
	};
	
	public TrackContactAdapter(Context context,List<UserInfo> list) {
		this.context = context;
		this.list = list;
		if(list !=null){
			states = new boolean[list.size()];
		}
	}
	
	public void setData(List<UserInfo> list){
		this.list = list;
		if(list !=null){
			states = new boolean[list.size()];
		}
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void setState(int position ,boolean state) {
		states[position] = state;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null ;
		if ( convertView == null ) {
			convertView = LayoutInflater.from(context).inflate(R.layout.user_item_track, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.tv_user_name);
			holder.userIcon = (ImageView) convertView.findViewById(R.id.iv_user_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.userIcon.setImageResource(R.drawable.friends);
		holder.name.setText(list.get(position).getName());
		holder.name.setBackgroundResource(BACKGROUNDS[position%BACKGROUNDS.length]);
		if (states[position]) {
			holder.name.setSelected(true);
		} else {
			holder.name.setSelected(false);
		}
		return convertView;
	}
	
	public void resetAdapter(){
		Arrays.fill(states, false);
		notifyDataSetChanged();
	}
	
	class ViewHolder {
		public TextView  name;
		public ImageView userIcon;
	}
}
